#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <set>
#include <bitset>
#include <utility>
using namespace std;

#define mm(a, n) memset(a, n, sizeof a)
#define mk(a, b) make_pair(a, b)

const double eps = 1e-6;
const int INF = 0x3f3f3f3f;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef pair<LL, LL> PLL;
typedef pair<int, LL> PIL;

inline void quickread() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
}

const int N = 50;

bool st[N];
int l[N], r[N];
int n;
vector<int> level_order, in_order;

void BFS(int u) {
    queue<int> q;
    q.push(u);
    while (q.size()) {
        int head = q.front(); q.pop();
        if (r[head] != -1) q.push(r[head]);
        if (l[head] != -1) q.push(l[head]);
        level_order.push_back(head);
    }
}

void DFS(int u) {
    if (r[u] != -1) DFS(r[u]);
    in_order.push_back(u);
    if (l[u] != -1) DFS(l[u]);
}
 
inline void solution() {
    mm(l, -1);
    mm(r, -1);
    scanf("%d\n", &n);
    for (int i = 0; i < n; i ++ ) {
        char lc[5], rc[5];
        scanf("%s%s", lc, rc);
        if (lc[0] != '-') 
            l[i] = lc[0] - '0', st[lc[0] - '0'] = true;
        if (rc[0] != '-') 
            r[i] = rc[0] - '0', st[rc[0] - '0'] = true;
    }
    // find root node
    int root;
    for (int i = 0; i < n; i ++ )
        if (st[i] == false)
            root = i;
    // level_order;
    BFS(root);
    // in_order;
    DFS(root);

    cout << level_order[0];
    for (int i = 1; i < level_order.size(); i ++ ) cout << " " << level_order[i];
    cout << endl;

    cout << in_order[0];
    for (int i = 1; i < in_order.size(); i ++ ) cout << " " << in_order[i];
    cout << endl;
}

int main() {
    freopen("input.txt", "r", stdin);
    quickread();
    solution();
    return 0;
}